package com.supplychainapi.utils;

import com.google.common.collect.Lists;
import com.supplychainapi.domain.TbtreebuildParam;
import com.supplychainapi.model.ScmTbtreebuild;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections4.comparators.ComparatorChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.List;

public class SortUtil {
    private static Logger  logger =  LoggerFactory.getLogger(SortUtil.class);

    @SuppressWarnings("unchecked")
    /**
     * 根据分类fiSort 正序排序
     */
    public static void treeBuildsortByfiSort(List list){
        try{
            ComparatorChain moInfoComparator = new ComparatorChain();
            moInfoComparator.addComparator(new BeanComparator("fiSort"), false);
            Collections.sort(list, moInfoComparator);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }


    public static void RecursiveList(List<ScmTbtreebuild> list, List result){
        if(null==list && list.isEmpty())
            return;
//        List<TbtreebuildParam> result=Lists.newArrayList();
        for (ScmTbtreebuild a : list) {
            TbtreebuildParam param=new TbtreebuildParam();
            BeanUtils.copyProperties(a,param);
            if(null!= a.getScmTbtreebuildList()){
                List aList=Lists.newArrayList();
                for (ScmTbtreebuild b : a.getScmTbtreebuildList()) {
                    TbtreebuildParam aParam= new TbtreebuildParam();
                    BeanUtils.copyProperties(b,aParam);
                    if(null!= b.getScmTbtreebuildList()){
                        List bList=Lists.newArrayList();
                        for (ScmTbtreebuild c : b.getScmTbtreebuildList()) {
                            TbtreebuildParam bParam= new TbtreebuildParam();
                            BeanUtils.copyProperties(c,bParam);
                            bList.add(bParam);
                        }
                        aParam.setScmTbtreebuildList(bList);
                    }
                    aList.add(aParam);
                }
                param.setScmTbtreebuildList(aList);
            }
            result.add(param);
        }
    }

    public static void main(String[] args) {
        List<ScmTbtreebuild> list=Lists.newArrayList();
        ScmTbtreebuild a=new ScmTbtreebuild();
        ScmTbtreebuild b=new ScmTbtreebuild();

        b.setFiSort((short) 2);
        a.setFiSort((short) 1);
        list.add(b);
        list.add(a);
        for (ScmTbtreebuild tbtreebuild : list) {
            System.out.println(tbtreebuild.getFiSort());
        }
        SortUtil.treeBuildsortByfiSort(list);
        for (ScmTbtreebuild tbtreebuild : list) {
            System.out.println(tbtreebuild.getFiSort());
        }
    }
}
